package com.usfuture.chart.techanly.scale
{

	import com.usfuture.chart.IChart;
	import com.usfuture.chart.techanly.AbstractTechProperty;
	import com.usfuture.chart.techanly.ITechAnlyChart;
	import com.usfuture.techanly.entity.Line;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;

	
	public class ScaleChart extends EventDispatcher implements ITechAnlyChart
	{
		private var _canvas:Sprite;
		private var _data:Array;
		private var canvasWidth:Number;
		private var canvasHeigth:Number;
		private var _leftWidthX:Number;//K线图距画布左边的距离
		private var _width:Number;
		private var _starPoint:Point;//K线开始位置
		private var _hours:Array = new Array();//已画过的星期线
		private var _days:Array = new Array();//已画过的星期线
		private var _weeks:Array = new Array();//已画过的星期线
		private var _months:Array = new Array();//已画过的月线
		private var _years:Array = new Array();//已画过的年线
		private var _techProperty:AbstractTechProperty;

		
		public function ScaleChart(canvas:Sprite)
		{
			
			this._canvas = canvas;
			measure();
		}
		
		public function measure():void{
			this.canvasWidth = _canvas.width;
			this.canvasHeigth = _canvas.height-1;
			this._leftWidthX = AbstractTechProperty.leftWidthX;
			this._width=canvasWidth-_leftWidthX;
			this._starPoint = new Point(_leftWidthX,0);
		}
		
		public function paint():void
		{
			if(!_data||_data.length==0)
				return;
			this._canvas.graphics.lineStyle(1,0x716857,1);
			switch(AbstractTechProperty.lineType){
				case AbstractTechProperty.LINE_ONE_MINUTE:
					if((_width/techProperty.lineNum)*30>100){
						paintGssssradeMinHour();
					}else
						paintGssssradeMinDay();
					break;
				case AbstractTechProperty.LINE_FIVE_MINUTE:
					if((_width/techProperty.lineNum)*30>100){
						paintGssssradeMinHour();
					}else
						paintGssssradeMinDay();
					break;
				case AbstractTechProperty.LINE_TEN_MINUTE:
					if((_width/techProperty.lineNum)*30>100){
						paintGssssradeMinHour();
					}else
						paintGssssradeMinDay();
					break;
				case AbstractTechProperty.LINE_FIFTEEN_MINUTE:
					if((_width/techProperty.lineNum)*30>100){
						paintGssssradeMinHour();
					}else
						paintGssssradeMinDay();
					break;
				case AbstractTechProperty.LINE_THIRTY_MINUTE:
					if((_width/techProperty.lineNum)*30>100){
						paintGssssradeMinHour();
					}else
						paintGssssradeMinDay();
					break;
				case AbstractTechProperty.LINE_SIXTY_MINUTE:
					if((_width/techProperty.lineNum)*30>100){
						paintGssssradeMinHour();
					}else
						paintGssssradeMinDay();
					break;
				case AbstractTechProperty.LINE_DAILY:
					if(techProperty.mode)
						paintGradeMinWeek();
					else if((_width/techProperty.lineNum)*30>100){
						paintGradeMinMonth();
					}else {
						paintGradeMinYear();
					}
					break;
				case AbstractTechProperty.LINE_WEEK:
					if((_width/techProperty.lineNum)*30>250){
						paintGradeMinMonth();
					}else {
						paintGradeMinYear();
					}
					break;
				case AbstractTechProperty.LINE_MONTH:
					paintGradeMinYear();
					break;
				default:break;
			}

		}
		
		/**
		 * 最小单位为十分钟线
		 * 
		 **/
		public function paintGssssradeMinHalfHour():void
		{
			var dayArr:Array=new Array;
			for(var i:int=(techProperty.startPos-techProperty.lineNum<0?0:techProperty.startPos-techProperty.lineNum),j:int=0;i<techProperty.startPos;i++,j++){
				if(i<0) break;
				var date:Date=Line(_data[i]).timestamp;
				//画小时线
				if(!isExitOnArray(_hours,date.getDate()+''+date.getHours())){
					_hours.push(date.getDate()+''+date.getHours());
					var x:Number;
					if(techProperty.mode)
						x=_starPoint.x+(j+1)*(techProperty.lineWidth+techProperty.lineMergin)-(techProperty.lineWidth/2+techProperty.lineMergin);
					else
						x=_starPoint.x+(j+1)*(_width/techProperty.lineNum);
//					this._canvas.graphics.moveTo(x,5);
//					this._canvas.graphics.lineTo(x,10);
					var text:TextField = new TextField();
					//画小时线
					text.htmlText = '<font color="#716857">'+(date.getDate()+':'+date.getHours())+'</font>';
					_years.push(date.getFullYear());
					text.x = x;
//					text.y = 6;
					this._canvas.addChild(text); 
				}
				//画天线
				if(!isExitOnArray(_days,date.getDate().toString())){
					_days.push(date.getDate().toString());
					var x:Number;
					if(techProperty.mode)
						x=_starPoint.x+(j+1)*(techProperty.lineWidth+techProperty.lineMergin)-(techProperty.lineWidth/2+techProperty.lineMergin);
					else
						x=_starPoint.x+(j+1)*(_width/techProperty.lineNum);
//					this._canvas.graphics.moveTo(x,5);
//					this._canvas.graphics.lineTo(x,10);
					var text:TextField = new TextField();
					//画天线
					text.htmlText = '<font color="#716857">'+((date.getMonth()+1)+"/"+date.getDate())+'</font>';
					_years.push(date.getFullYear());
					text.x = x;
//					text.y = 6;
					this._canvas.addChild(text); 
				}
				
			}
		}
		
		/**
		 * 最小单位为小时
		 * 
		 **/
		public function paintGssssradeMinHour():void
		{
			var dayArr:Array=new Array;
			for(var i:int=(techProperty.startPos-techProperty.lineNum<0?0:techProperty.startPos-techProperty.lineNum),j:int=0;i<techProperty.startPos;i++,j++){
				if(i<0) break;
				var date:Date=Line(_data[i]).timestamp;
				//画小时线
				if(!isExitOnArray(_hours,date.getDate()+''+date.getHours())){
					_hours.push(date.getDate()+''+date.getHours());
					var x:Number;
					if(techProperty.mode)
						x=_starPoint.x+(j+1)*(techProperty.lineWidth+techProperty.lineMergin)-(techProperty.lineWidth/2+techProperty.lineMergin);
					else
						x=_starPoint.x+(j+1)*(_width/techProperty.lineNum);
//					this._canvas.graphics.moveTo(x,5);
//					this._canvas.graphics.lineTo(x,10);
				}
				//画天线
				if(!isExitOnArray(_days,date.getDate().toString())){
					_days.push(date.getDate().toString());
					var x:Number;
					if(techProperty.mode)
						x=_starPoint.x+(j+1)*(techProperty.lineWidth+techProperty.lineMergin)-(techProperty.lineWidth/2+techProperty.lineMergin);
					else
						x=_starPoint.x+(j+1)*(_width/techProperty.lineNum);
//					this._canvas.graphics.moveTo(x,5);
//					this._canvas.graphics.lineTo(x,10);
					var text:TextField = new TextField();
					//画天线
					text.htmlText = '<font color="#716857">'+((date.getMonth()+1)+"/"+(date.getDate()))+'</font>';
					_years.push(date.getFullYear()+'');
					text.x = x;
//					text.y = 6;
					this._canvas.addChild(text); 
				}
				
			}
		}
		
		/**
		 * 最小单位为天数
		 * 
		 **/
		public function paintGssssradeMinDay():void
		{
			var dayArr:Array=new Array;
			for(var i:int=(techProperty.startPos-techProperty.lineNum<0?0:techProperty.startPos-techProperty.lineNum),j:int=0;i<techProperty.startPos;i++,j++){
				if(i<0) break;
				var date:Date=Line(_data[i]).timestamp;
				//画天线
				if(!isExitOnArray(_days,date.getDate().toString())){
					_days.push(date.getDate().toString());
					var x:Number;
					if(techProperty.mode)
						x=_starPoint.x+(j+1)*(techProperty.lineWidth+techProperty.lineMergin)-(techProperty.lineWidth/2+techProperty.lineMergin);
					else
						x=_starPoint.x+(j+1)*(_width/techProperty.lineNum);
//					this._canvas.graphics.moveTo(x,5);
//					this._canvas.graphics.lineTo(x,10);
					var text:TextField = new TextField();
					//画天线
					text.htmlText = '<font color="#716857">'+((date.getMonth()+1)+"/"+(date.getDate()))+'</font>';
					_years.push(date.getFullYear()+'');
					text.x = x;
//					text.y = 6;
					this._canvas.addChild(text); 
				}
				
			}
		}
		

		
		/**
		 * 最小单位为星期线
		 */
		public function paintGradeMinWeek():void
		{
			for(var i:int=(techProperty.startPos-techProperty.lineNum<0?0:techProperty.startPos-techProperty.lineNum),j:int=0;i<techProperty.startPos;i++,j++){
				if(i<0) break;
				var date:Date=Line(_data[i]).timestamp;
				//画星期线
				if(date.dayUTC==0){
//					if(techProperty.mode){
//						this._canvas.graphics.moveTo(_starPoint.x+(j+1)*(techProperty.lineWidth+techProperty.lineMergin)-(techProperty.lineWidth/2+techProperty.lineMergin),5);
//						this._canvas.graphics.lineTo(_starPoint.x+(j+1)*(techProperty.lineWidth+techProperty.lineMergin)-(techProperty.lineWidth/2+techProperty.lineMergin),10);
//					}else{
//						this._canvas.graphics.moveTo(_starPoint.x+(j+1)*(_width/techProperty.lineNum),5);
//						this._canvas.graphics.lineTo(_starPoint.x+(j+1)*(_width/techProperty.lineNum),10);
//					}
				}
				//画月和年
				if(!isExitOnArray(_months,date.getFullYear()+""+date.getMonth())){
					var x:Number;
					if(techProperty.mode)
						x=_starPoint.x+(j+1)*(techProperty.lineWidth+techProperty.lineMergin)-(techProperty.lineWidth/2+techProperty.lineMergin);
					else
						x=_starPoint.x+(j+1)*(_width/techProperty.lineNum);
//					this._canvas.graphics.moveTo(x,5);
//					this._canvas.graphics.lineTo(x,10);
					var text:TextField = new TextField();
					//画年线
					if(!isExitOnArray(_years,date.getFullYear()+'')){
						text.htmlText = '<font color="#716857">'+(date.getFullYear().toString().substr(2,2)+"/"+(date.getMonth()+1))+'</font>';
						_years.push(date.getFullYear()+'');
						text.x = x;
					}else{
						text.htmlText = '<font color="#716857">'+(date.getMonth()+1)+'</font>';
						text.x = x;
					}
					
//					text.y = 6;
					this._canvas.addChild(text); 
					_months.push(date.getFullYear()+""+date.getMonth());
				}
				
			}
		}
		
		/**
		 * 最小单位为月线
		 */
		public function paintGradeMinMonth():void
		{
			for(var i:int=(techProperty.startPos-techProperty.lineNum<0?0:techProperty.startPos-techProperty.lineNum),j:int=0;i<techProperty.startPos;i++,j++){
				if(i<0) break;
				var date:Date=Line(_data[i]).timestamp;
				//画月和年
				if(!isExitOnArray(_months,date.getFullYear()+''+date.getMonth())){
					var x:Number;
					if(techProperty.mode)
						x=_starPoint.x+(j+1)*(techProperty.lineWidth+techProperty.lineMergin)-(techProperty.lineWidth/2+techProperty.lineMergin);
					else
						x=_starPoint.x+(j+1)*(_width/techProperty.lineNum);
//					this._canvas.graphics.moveTo(x,5);
//					this._canvas.graphics.lineTo(x,10);
					var text:TextField = new TextField();
					//画年线
					if(!isExitOnArray(_years,date.getFullYear().toString())){
						text.htmlText = '<font color="#716857">'+(date.getFullYear().toString().substr(2,2)+"/"+(date.getMonth()+1))+'</font>';
						_years.push(date.getFullYear().toString());
						text.x = x;
					}else{
						text.htmlText = '<font color="#716857">'+(date.getMonth()+1)+'</font>';
						text.x = x;
					}
//					text.y = 6;
					this._canvas.addChild(text); 
					_months.push(date.getFullYear()+''+date.getMonth());
				}
				
			}
		}
		
		/**
		 * 最小单位为年线
		 */
		public function paintGradeMinYear():void
		{
			for(var i:int=(techProperty.startPos-techProperty.lineNum<0?0:techProperty.startPos-techProperty.lineNum),j:int=0;i<techProperty.startPos;i++,j++){
				if(i<0) break;
				var date:Date=Line(_data[i]).timestamp;
				//画月和年
				if(!isExitOnArray(_years,date.getFullYear().toString())){
					var x:Number;
					if(techProperty.mode)
						x=_starPoint.x+(j+1)*(techProperty.lineWidth+techProperty.lineMergin)-(techProperty.lineWidth/2+techProperty.lineMergin);
					else
						x=_starPoint.x+(j+1)*(_width/techProperty.lineNum);
//					this._canvas.graphics.moveTo(x,5);
//					this._canvas.graphics.lineTo(x,10);
					var text:TextField = new TextField();
					text.htmlText = '<font color="#716857">'+(date.getFullYear().toString().substr(2,2)+"/"+(date.getMonth()+1))+'</font>';
					text.x = x;
//					text.y = 6;
					this._canvas.addChild(text); 
					_years.push(date.getFullYear().toString());
				}
				
			}
		}
		
		public function repaint():void
		{
			_hours.length=0;
			_days.length=0;
			_weeks.length=0;
			_months.length=0;
			_years.length=0;
			while(_canvas.numChildren>0){
				this._canvas.removeChildAt(0);
			}
			this._canvas.graphics.clear();
			this.paint();
		}
		

		public function get data():Array
		{
			return _data;
		}

		public function set data(value:Array):void
		{
			_data = value;
		}
		
		private function isExitOnArray(arr:Array,str:String):Boolean{
			for each(var str1:String in arr){
				if(str1 == str){
					return true;
				}
			}
			return false;
		}

		public function get techProperty():AbstractTechProperty
		{
			return _techProperty;
		}

		public function set techProperty(value:AbstractTechProperty):void
		{
			_techProperty = value;
			_techProperty.removeEventListener(AbstractTechProperty.CHANGE,change);
			_techProperty.addEventListener(AbstractTechProperty.CHANGE,change);
		}
		
		private function change(event:Event):void{
			repaint();
		}

	}
}